<?php
/**
 * @author 张伟
 * @date 2008.9.6
 * @summary 处理用户留言操作的动作类。
 */
class Leave_MessageAction extends Action{
	private $leave_messag;

	public function __construct()
	{
		parent::__construct();

		$this->leave_messag = new Leave_MessageModel();

		if($this->session->getAttr("uid") == null){
			$this->forward("Index");
		}
	}
	
	protected function init()
	{

	}
	public function display()
	{
		
	}
	/**************************/
	/*       增加一条留言     */
	/**************************/
	public function addMessage()
	{
		$msg_from_id = $this->session->getAttr('uid');
		$msg_to_id   = $_POST['uid'];
		$msg_owner   = $_POST['owner'];
		$msg_time    = date('Y-m-d h:i:s');
		$msg_text    = $_POST['message'];
		
		echo "msg_from_id : ".$msg_from_id."<br>";
		echo "msg_to_id   : ".$msg_to_id."<br>";
		echo "msg_owner   : ".$msg_owner."<br>";
		echo "msg_text    : ".$msg_text."<br>";

		if(!$msg_text)
		{
			$this->showMessage("留言不能为空！");
		}
		else
		{
			$msg_to_id = (int) $msg_to_id;

			if($msg_to_id <= 0)
			{
				$this->showMessage("执行了非法操作！");
			}
			else
			{
				if($msg_to_id==$msg_from_id)
				{
					$this->showMessage("不能给自己留言");
				}
				else
				{
					$this->leave_messag->msg_from_id=$msg_from_id;
					$this->leave_messag->msg_to_id=$msg_to_id;
					$this->leave_messag->msg_owner=$msg_owner;
					$this->leave_messag->msg_time=$msg_time;
					$this->leave_messag->msg_text=$msg_text;
					$this->leave_messag->add();
					$this->showMessage("留言成功！");
				}
			}
		}

	} 
	/**************************/
	/*  删除msg_id为mid留言   */
	/**************************/
	public function delMessage()
	{
		$msg_id=$_POST['mid'];

		$this->leave_messag->addCondition('msg_id', '=', $msg_id, "");
		$find = $this->leave_messag->findOne();
		if($find)
		{
			$this->leave_messag->addCondition('msg_id', '=', $msg_id, "");
			$this->leave_messag->delete();
		}
		else
		{
			$this->showMessage("删除失败！留言不存在");
		}
	}
	/**************************/
	/*       显示留言列表     */
	/**************************/
	public function doView()
	{
		$this->leave_messag->addCondition('msg_owner', '=', $_POST['uid'], "or");
		$this->leave_messag->addCondition('msg_to_id', '=', $_POST['uid'], "");
		$apps = $this->leave_messag->find();

		$this->smarty->assign('apps', $apps);
		$this->smarty->display("Message_List.tpl");
	}
};
?>

